import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme; 
import java.util.Scanner;
import java.lang.*;
import java.io.*;
import java.util.*;
public class Segment{
	public static void main(String argv[]){
		
		try{
	//		System.out.println(argv[0]);
//			System.out.println(argv[1]);
            if (argv.length < 2) {
                System.out.println("Usage: Segment srcFile dstFile");
                System.exit(1);
            }
			FileReader fr=new FileReader(argv[0]); //输入源？
			BufferedReader br=new BufferedReader(fr);
            FileWriter fw=new FileWriter(argv[1]);                                     
            BufferedWriter bw=new BufferedWriter(fw); 
			String line=null;
			Map<String,Integer> map=new HashMap<String,Integer>();
			while((line=br.readLine())!=null){
				//System.out.println(line);
				StringReader rd=new StringReader(line);
				IKSegmenter seg=new IKSegmenter(rd,true);
				Lexeme word=null;
				while((word=seg.next())!=null){
					String wordStr=word.getLexemeText();
					bw.write(wordStr+" ");
					if(map.containsKey(wordStr)){
						map.put(wordStr,map.get(wordStr).intValue()+1);
					}else{
						map.put(wordStr,new Integer(1));
					}
				}
				bw.newLine();
			}
            fr.close();
            bw.close();
	    	/*	
			Iterator iter=map.entrySet().iterator(); 
            FileWriter fw=new FileWriter(argv[0]+"opt.log");
			BufferedWriter bw=new BufferedWriter(fw);
			while(iter.hasNext()){
				Map.Entry<String,Integer> entry=(Map.Entry)iter.next();
				String str=(String)entry.getKey();
				int  inte=entry.getValue().intValue();
				String strLine=str+" "+inte;
				bw.write(strLine);
				bw.newLine();
				bw.flush();
			}
            */
			
		}catch (FileNotFoundException e) {
			e.printStackTrace();
			
		}catch (IOException e) {
			e.printStackTrace();
			
		}catch (Exception e) {
			e.printStackTrace();
			
		}
	}
}
